package cn.iocoder.yudao.module.common.controller.admin.tagrelation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.common.controller.admin.tagrelation.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.tagrelation.TagRelationDO;
import cn.iocoder.yudao.module.common.service.tagrelation.TagRelationService;

@Tag(name = "管理后台 - 标签关系")
@RestController
@RequestMapping("/common/tag-relation")
@Validated
public class TagRelationController {

    @Resource
    private TagRelationService tagRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建标签关系")
    @PreAuthorize("@ss.hasPermission('common:tag-relation:create')")
    public CommonResult<Long> createTagRelation(@Valid @RequestBody TagRelationSaveReqVO createReqVO) {
        return success(tagRelationService.createTagRelation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新标签关系")
    @PreAuthorize("@ss.hasPermission('common:tag-relation:update')")
    public CommonResult<Boolean> updateTagRelation(@Valid @RequestBody TagRelationSaveReqVO updateReqVO) {
        tagRelationService.updateTagRelation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除标签关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('common:tag-relation:delete')")
    public CommonResult<Boolean> deleteTagRelation(@RequestParam("id") Long id) {
        tagRelationService.deleteTagRelation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得标签关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('common:tag-relation:query')")
    public CommonResult<TagRelationRespVO> getTagRelation(@RequestParam("id") Long id) {
        TagRelationDO tagRelation = tagRelationService.getTagRelation(id);
        return success(BeanUtils.toBean(tagRelation, TagRelationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得标签关系分页")
    @PreAuthorize("@ss.hasPermission('common:tag-relation:query')")
    public CommonResult<PageResult<TagRelationRespVO>> getTagRelationPage(@Valid TagRelationPageReqVO pageReqVO) {
        PageResult<TagRelationDO> pageResult = tagRelationService.getTagRelationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TagRelationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出标签关系 Excel")
    @PreAuthorize("@ss.hasPermission('common:tag-relation:export')")
    @OperateLog(type = EXPORT)
    public void exportTagRelationExcel(@Valid TagRelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TagRelationDO> list = tagRelationService.getTagRelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "标签关系.xls", "数据", TagRelationRespVO.class,
                        BeanUtils.toBean(list, TagRelationRespVO.class));
    }

}